package cn.px.rest.controller.store;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.store.SpareRestoreFormDTO;
import cn.px.dao.entity.store.SpareRestoreForm;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.store.ExportSpareRestoreFormVO;
import cn.px.dao.vo.query.store.SpareRestoreFormVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.store.SpareRestoreFormService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 备件退库登记单 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/store/spare-restore-forms")
public class SpareRestoreFormController {

  private final  SpareRestoreFormService sparePickFormService;

  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<SpareRestoreFormVO> getById(@PathVariable("id") String id){
      return new R<>(sparePickFormService.getDetail(id));
  }

  /**
   * 新增记录
   *
   * @param sparePickFormDTO 备件退库登记单DTO
   * @return R
   */
  @SysLog("新增备件退库登记单")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('store-spare-restore-forms-add')")
  public R save(@Validated({AddGroup.class}) @RequestBody SpareRestoreFormDTO sparePickFormDTO) {
      return new R<>(sparePickFormService.saveModelByDTO(sparePickFormDTO));
  }

  /**
   * 修改记录
   *
   * @param sparePickFormDTO 备件退库登记单DTO
   * @return R
   */
  @SysLog("修改备件退库登记单")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('store-spare-restore-forms-edit')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody SpareRestoreFormDTO sparePickFormDTO) {
      sparePickFormService.updateModelByDTO(sparePickFormDTO);
      return new R<>();
  }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除备件退库登记单")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('store-spare-restore-forms-del')")
  public R removeById(@PathVariable String id){
    sparePickFormService.cascadingDeleteByKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除备件退库登记单")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('store-spare-restore-forms-del')")
    public R removeIds(@RequestBody List<String> ids){
        sparePickFormService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param sparePickFormDTO 备件退库登记单DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<SpareRestoreFormVO>> query(SpareRestoreFormDTO sparePickFormDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      return new R<>(sparePickFormService.selectPageList(sparePickFormDTO, pageNum, pageSize));
  }

  /**
   * 获取列表
   *
   * @param sparePickFormDTO 备件退库登记单DTO
   * @return R
   */
  @GetMapping("")
  public R query(SpareRestoreFormDTO sparePickFormDTO) {
      return new R<>(sparePickFormService.getModelListByDTO(sparePickFormDTO));
  }

  /**
     * 备件退库登记单导出
     *
     * @param sparePickFormDTO 备件退库登记单DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("备件退库登记单导出")
  @PreAuthorize("@pms.hasPermission('store-spare-restore-forms-export')")
  public void export(HttpServletResponse response, SpareRestoreFormDTO sparePickFormDTO) {
    List<SpareRestoreForm> list = sparePickFormService.getModelListByDTO(sparePickFormDTO);
    ExcelUtil.exportResponseDict(response, ExportSpareRestoreFormVO.class, BeanConverterUtil.copyListProperties(list, ExportSpareRestoreFormVO.class), "备件退库登记单");
  }

}
